package com.jq.aution.service.impl;

import com.jq.aution.mapper.UserAccountInfoMapper;
import com.jq.aution.mapper.UserInfoMapper;
import com.jq.aution.model.UserAccountInfo;
import com.jq.aution.model.UserInfo;
import com.jq.aution.model.UserInfoExample;
import com.jq.aution.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserAccountInfoMapper userAccountInfoMapper;

    @Override
    public boolean login(String username, String password) {
        try {
            UserInfo userInfo=loadUserByUsername(username);
            if(userInfo.getUserPassword().equals(password)){
                return true;
            }
        }catch (Exception e){
            LOGGER.error("用户空指针异常:{}",e.getMessage());
        }
        LOGGER.warn("用户名或密码错误");
        return false;
    }

    @Override
    public boolean register(String username, String password, String userType) {
        UserInfo userInfo=loadUserByUsername(username);
        if(userInfo!=null){
            LOGGER.info("该用户名已经被注册");
            return false;
        }else{

            UserInfo newUserInfo=new UserInfo();
            newUserInfo.setUserName(username);
            newUserInfo.setUserPassword(password);
            newUserInfo.setUserType(userType);

            if(userInfoMapper.insertSelective(newUserInfo)==1){
                LOGGER.info("注册成功");
                userInfo=loadUserByUsername(username);

                UserAccountInfo newUserAcct=new UserAccountInfo();
                newUserAcct.setUserId(userInfo.getUserId());
                newUserAcct.setAccount(1000f);
                newUserAcct.setCreditValue(100);

                if(userAccountInfoMapper.insertSelective(newUserAcct)==1){
                    LOGGER.info("插入用户账户信息成功");
                }else{
                    LOGGER.error("插入用户信息出错");
                }
                return true;
            }
            return false;
        }
    }

    @Override
    public UserInfo loadUserByUsername(String username){
        //获取用户信息
        UserInfoExample example=new UserInfoExample();
        example.createCriteria().andUserNameEqualTo(username);
        List<UserInfo> userInfoList=userInfoMapper.selectByExample(example);

        if(userInfoList!=null && userInfoList.size()==1){
            return userInfoList.get(0);
        }
        else {
            LOGGER.info("没有查找到该用户");
            return null;
        }
    }

    @Override
    public UserInfo loadUserById(Integer id) {
        //获取用户信息
        UserInfoExample example=new UserInfoExample();
        example.createCriteria().andUserIdEqualTo(id);
        List<UserInfo> userInfoList=userInfoMapper.selectByExample(example);

        if(userInfoList!=null && userInfoList.size()==1){
            return userInfoList.get(0);
        }
        else {
            LOGGER.info("没有查找到该用户");
            return null;
        }
    }
}
